package be.gnx.fogo.application.ui;

import java.util.ResourceBundle;
import java.util.Vector;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;

import be.gnx.fogo.application.event.SearchBarModifiedEvent;
import be.gnx.fogo.application.event.SearchBarModifiedListener;

public class SearchBar extends Composite {

  private Vector<SearchBarModifiedListener> searchBarModifiedListeners = new Vector<SearchBarModifiedListener>();

  private Combo searchCombo;

  private Button asFamilyButton;

  private Button personButton;

  private Button organizationButton;

  public SearchBar(Composite composite, int style, ResourceBundle resourceBundle) {
    super(composite, style);
    this.setLayout(new FillLayout());
    searchCombo = new Combo(this, SWT.NONE);
    asFamilyButton = new Button(this, SWT.CHECK);
    personButton = new Button(this, SWT.CHECK);
    organizationButton = new Button(this, SWT.CHECK);

    asFamilyButton.setText("as family");
    personButton.setText("persons");
    organizationButton.setText("organizations");
    resize();

    searchCombo.addModifyListener(new ModifyListener() {
      public void modifyText(ModifyEvent modifyEvent) {
        sendEvents();
      }
    });
    
    asFamilyButton.addSelectionListener(new SelectionListener() {
      public void widgetDefaultSelected(SelectionEvent selectionEvent) {
        sendEvents();
      }

      public void widgetSelected(SelectionEvent selectionEvent) {
        sendEvents();
      }
    });
    
    personButton.addSelectionListener(new SelectionListener() {
      public void widgetDefaultSelected(SelectionEvent selectionEvent) {
        sendEvents();
      }

      public void widgetSelected(SelectionEvent selectionEvent) {
        sendEvents();
      }
    });
    
    organizationButton.addSelectionListener(new SelectionListener() {
      public void widgetDefaultSelected(SelectionEvent selectionEvent) {
        sendEvents();
      }

      public void widgetSelected(SelectionEvent selectionEvent) {
        sendEvents();
      }
    });
    
    addDisposeListener(new DisposeListener() {
      public void widgetDisposed(DisposeEvent disposeEvent) {
        SearchBar.this.widgetDisposed(disposeEvent);
      }
    });

    addControlListener(new ControlAdapter() {
      public void controlResized(ControlEvent controlEvent) {
        SearchBar.this.controlResized(controlEvent);
      }
    });

  }

  public void addSearchBarModifiedListener (SearchBarModifiedListener searchBarModifiedListener) {
    checkWidget();
    if (searchBarModifiedListener == null) {
      SWT.error(SWT.ERROR_NULL_ARGUMENT);
    } else {
      searchBarModifiedListeners.add(searchBarModifiedListener);
    }
  }

  public void removeSearchBarModifiedListener (SearchBarModifiedListener searchBarModifiedListener) {
    checkWidget();
    if (searchBarModifiedListener == null) {
      SWT.error(SWT.ERROR_NULL_ARGUMENT);
    } else {
      searchBarModifiedListeners.remove(searchBarModifiedListener);
    }
  }

  private void widgetDisposed(DisposeEvent disposeEvent) {
    // TODO;
  }

  private void controlResized(ControlEvent controlEvent) {
    resize();
  }

  private void resize() {
    Point searchComboSize = searchCombo.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
    Point asFamilyButtonSize = asFamilyButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
    Point personButtonSize = personButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
    Point organizationButtonSize = organizationButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, false);
    searchCombo.setBounds(0, 0, searchComboSize.x, searchComboSize.y);
  }

  private void sendEvents() {
    SearchBarModifiedEvent event = new SearchBarModifiedEvent();
    event.type = SWT.Modify;
    event.display = getDisplay();
    event.widget = SearchBar.this;
    event.setSearchCombo(searchCombo);
    event.setAsFamily(asFamilyButton.getSelection());
    for (SearchBarModifiedListener searchBarModifiedListener : searchBarModifiedListeners) {
      searchBarModifiedListener.handleEvent(event);
    }
  }

}
